package com.github.icloudpay.pay.core.service.pay.other.service;

import java.math.BigDecimal;
import java.util.Map;

import com.github.wxiaoqi.security.biz.common.admin.pay.request.CallbackRequest;
import com.github.wxiaoqi.security.biz.common.admin.pay.response.PayHelperReponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.icloudpay.pay.core.biz.PaySerialBiz;
import com.github.icloudpay.pay.core.entity.PayOrder;
import com.github.icloudpay.pay.core.entity.PaySerial;
import com.github.icloudpay.pay.core.mapper.PayOrderMapper;
import com.github.icloudpay.pay.core.service.pay.PayCallbackService;

/**
 * 支付助手app支付回调
 * @author he
 */
@Service("payHelperPayCallBackService")
public class PayHelperPayCallBackService {

	private static final Logger logger = (Logger) LoggerFactory.getLogger(PayHelperPayCallBackService.class);

	@Autowired
	private PaySerialBiz paySerialBiz;
	@Autowired
	private PayCallbackService payCallbackService;
	@Autowired
	private PayOrderMapper payOrderMapper;

	public PayHelperReponse payCallBack(Map<String, Object> request){

		PayHelperReponse callbackReponse = new PayHelperReponse();

		String out_trade_no = (String) request.get("out_trade_no");
		String qr_status = (String) request.get("qr_status");// 0动态 1 静态
		String pay_account = (String) request.get("pay_account");//账号
		String nickname = (String) request.get("nickname");//昵称
		String total_fee = (String) request.get("total_fee");
		String realname = (String) request.get("realname");//真实姓名
		
		PaySerial selectPaySerial = null;
		if("1".equals(qr_status)){
			//静态码支付
			//判断真实姓名是否为空
			PayOrder selectPayOrder = null;
			if(StringUtils.isEmpty(realname)){
				//如果为空为个人二维码
				//根据金额，账号，昵称查询最新一条不超过一小时的订单
				PayOrder payOrder = new PayOrder();
				//截取昵称前三位
				pay_account = pay_account.substring(0,3);
				payOrder.setPayAccount(pay_account);
				payOrder.setNickname(nickname);
				payOrder.setPayAmt(new BigDecimal(total_fee));
				selectPayOrder = payOrderMapper.queryLastOrderByAccount(payOrder);
				
			}else{
				//不为空为商户二维码
				//根据金额，昵称查询最新一条不超过一小时的订单
				PayOrder payOrder = new PayOrder();
				//截取昵称最后一位
				realname = realname.substring(realname.length() - 1,realname.length());
				payOrder.setRealname(realname);
				payOrder.setPayAmt(new BigDecimal(total_fee));
				selectPayOrder = payOrderMapper.queryLastOrderByAccount(payOrder);
			}
			if(null == selectPayOrder){
				logger.error("订单不存在, out_trade_no{}",out_trade_no);
				callbackReponse.setReturn_code("FAIL");
				return callbackReponse;
			}
			String payOrderNo = selectPayOrder.getPayOrderNo();
			PaySerial paySerial = new PaySerial();
			paySerial.setPayOrderNo(payOrderNo);
			selectPaySerial = paySerialBiz.selectOne(paySerial);
			
		}else if("0".equals(qr_status)){
			PaySerial paySerial = new PaySerial();
			paySerial.setSerialNo(out_trade_no);
			selectPaySerial = paySerialBiz.selectOne(paySerial);
		}
		if(selectPaySerial == null){
			logger.error("查询支付流水为空, out_trade_no{}",out_trade_no);
			callbackReponse.setReturn_code("FAIL");
			return callbackReponse;
		}
		

		String trade_no = (String) request.get("transaction_id");

		CallbackRequest callbackRequest = new CallbackRequest();
		callbackRequest.setRequestNo(selectPaySerial.getSerialNo());
		callbackRequest.setOrderAmt(new BigDecimal(total_fee));
		callbackRequest.setRemoteTxJournalNo(trade_no);

		//验证成功
		callbackRequest.setStatus("00");
		payCallbackService.callBack(callbackRequest);
		callbackReponse.setReturn_code("SUCCESS");
		return callbackReponse;
	}

}
